package cn.tedu.micro.demo.order.service;

import cn.tedu.micro.demo.common.result.JsonResult;
import cn.tedu.micro.demo.order.dao.repository.OrderRepository;
import cn.tedu.micro.demo.order.feign.CartFeignClient;
import cn.tedu.micro.demo.order.feign.StockFeignClient;
import cn.tedu.micro.demo.pojo.po.OrderPO;
import cn.tedu.micro.demo.pojo.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService{
    /*@Autowired
    private StockRepository stockRepository;*/
    //update stock_tbl set stock=stock-#{count} where product_code=#{productCode}
    /*@Autowired
    private CartRepository cartRepository;*/
    //delete from cart_tbl where user_id=#{userId} and product_code=#{productCode}
    @Autowired
    private OrderRepository orderRepository;
    //insert into order_tbl(user_id,product_code,count) values(#{userId},#{productCode},#{count})
    @Autowired
    private CartFeignClient cartFeignClient;
    @Autowired
    private StockFeignClient stockFeignClient;
    @Override
    public void submitOrder(String userId, String productCode, Integer count) {
        System.out.println("当前购物车feign客户端类:"+cartFeignClient.getClass().getName());
        JsonResult<List<CartVO>> result = cartFeignClient.cartList("123");
        System.out.println("获取用户123的购物车数据:"+result.getData());
        //1.减库存
        //String url="http://18.9.0.19:8390/stock/reduce?productCode={1}&count={2}";
        stockFeignClient.updateStock(productCode,count);
        //stockRepository.updateStock(productCode,count);
        //2.增订单
        OrderPO po=new OrderPO();
        po.setUserId(userId);
        po.setCount(count);
        po.setProductCode(productCode);
        po.setOrderSn(System.currentTimeMillis()+"");
        orderRepository.save(po);
        //3.删除购物车
        cartFeignClient.deleteCart(userId, productCode);
        //cartRepository.deleteCart(userId,productCode);
    }
}
